<?php namespace App\Repositories\Eloquent\Item;

use App\Exceptions\OtherException;
use App\Models\Project;
use App\Models\ProjectHasContact;
use App\Repositories\Eloquent\Item\Entity\ProjectEntity;
use App\Repositories\Item\ProjectRepositoryInterface;

class ProjectRepository implements ProjectRepositoryInterface
{
    /**
     * 获取项目信息
     *
     * @param $id
     *
     * @return \Illuminate\Database\Eloquent\Collection|\App\Models\Project|\App\Models\Project[]
     */
    public function show($id)
    {
        return Project::query()->find($id);
    }

    /**
     * 新增项目
     *
     * @param ProjectEntity $entity
     *
     * @return Project
     */
    public function store(ProjectEntity $entity)
    {
        $bool = $this->checkNameRepeat($entity->creator_uid, $entity->name);
        throw_if($bool, new OtherException('项目名称重复'));

        $model = new Project($entity->toArray());
        throw_unless($model->save(), new OtherException('创建项目失败，请重试'));

        if ($entity->contact_name && $entity->contact_phone) {
            $model->contact()->save(new ProjectHasContact([
                'user_id' => $entity->creator_uid,
                'name'    => $entity->contact_name,
                'phone'   => $entity->contact_phone,
            ]));
        }

        return $model;
    }

    /**
     * 修改项目信息
     *
     * @param               $id
     * @param ProjectEntity $entity
     *
     * @return Project|Project[]|\Illuminate\Database\Eloquent\Collection
     */
    public function update($id, ProjectEntity $entity)
    {
        throw_unless($this->isMyProject($entity->creator_uid, (int)$id), new OtherException('没有权限修改'));

        $bool = $this->checkNameRepeat($entity->creator_uid, $entity->name, (int)$id);
        throw_if($bool, new OtherException('项目名称重复'));

        $project = $this->show($id);
        $project->fill($entity->toArray());
        throw_unless($project->save(), new OtherException('项目修改失败，请重试!'));

        $contact = $project->contact;
        //修改联系人
        if (!$contact || $contact->name != $entity->contact_name || $contact->phone != $entity->contact_phone) {
            if ($contact) {
                $contact->delete();
            }

            if ($entity->contact_name && $entity->contact_phone) {
                $project->contact()->save(new ProjectHasContact([
                    'user_id' => $entity->creator_uid,
                    'name'    => $entity->contact_name,
                    'phone'   => $entity->contact_phone,
                ]));
            }
        }

        return $project;
    }

    /**
     * 获取用户项目
     *
     * @param     $creator_uid
     * @param int $per_page
     *
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function lists($creator_uid, $per_page = 10)
    {
        return Project::query()
            ->where(compact('creator_uid'))
            ->latest()
            ->paginate($per_page);
    }

    /**
     * 是否自己的项目
     *
     * @param $creator_uid
     * @param $id
     *
     * @return bool
     *
     */
    private function isMyProject($creator_uid, $id)
    {
        return Project::query()->where(compact('creator_uid', 'id'))->count() > 0;
    }

    /**
     * 判断用户是否拥有该项目
     *
     * @param $user_id
     * @param $project_id
     *
     * @return bool
     */
    public function checkUserHasProject($user_id, $project_id)
    {
        return Project::query()
                ->where('creator_uid', $user_id)
                ->where('id', $project_id)
                ->count() > 0;
    }

    /**
     * 判断项目标题是否重复
     *
     * @param      $user_id
     * @param      $name
     * @param null $unless_id
     *
     * @return bool
     */
    private function checkNameRepeat($user_id, $name, $unless_id = null)
    {
        $query = Project::query();
        if (isset($unless_id)) {
            $query->where('id', '<>', $unless_id);
        }

        return $query->where('creator_uid', $user_id)
                ->where('name', $name)
                ->count() > 0;
    }
}
